package com.mrzhou.log.feign.factory;

import com.mrzhou.core.tool.api.R;
import com.mrzhou.core.tool.utils.JacksonUtil;
import com.mrzhou.log.feign.ILogFeignClient;
import com.mrzhou.log.feign.fallback.LogFeignFallback;
import feign.FeignException;
import feign.hystrix.FallbackFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @author qiang.zhou
 */
@Slf4j
public class LogFeignFactory implements FallbackFactory<ILogFeignClient> {
    @Override
    public ILogFeignClient create(Throwable throwable) {
        if(throwable instanceof FeignException) {
            FeignException exception = (FeignException) throwable;
            //获取返回的数据对象
            String json = exception.contentUTF8();
            // 项目异常或逻辑均会被统一返回R
            R result = JacksonUtil.parseObject(json, R.class);
            return new LogFeignFallback(result);
        }
        log.error("调用日志服务失败： {}", throwable);
        return new LogFeignFallback();
    }
}
